// $codepro.audit.disable
/**
 * package for all of the controller classes to fit MVC design pattern
 */
package javamonsters.controller;

import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javamonsters.model.Ship;
import javamonsters.model.SolarSystem;
import javamonsters.universe.application.UniverseController;
import javamonsters.view.SpaceTraders;
import javamonsters.view.UniverseSideView;



/**
 * 
 * Controller for the UniverseSideView class
 * 
 * @author Evan
 * @version 1.0
 */
public class UniverseSideViewController {
	
	/**
	 * dimension of the image representing a system
	 */
	private final double imageDimension;
	
	/**
	 * variables used to square numbers and calculate fuel
	 * relative to the distance, respectively
	 */
	private final int power, distanceDivider;
	
	/**
	 * universe side view
	 */
	private final UniverseSideView sideView;
	
	/**
	 * space traders object
	 */
	private final SpaceTraders spaceTraders;
	
	/**
	 * controller for the universe
	 */
	private final UniverseController universeController;
	
	/**
	 * @param sideView the universe side view
	 * @param spaceTraders the space traders object
	 * @param universeController the controller for the universe
	 */
	public UniverseSideViewController(UniverseSideView sideView, 
		   SpaceTraders spaceTraders, UniverseController universeController) {
		
		this.sideView = sideView;
		this.spaceTraders = spaceTraders;
		this.universeController = universeController;
		imageDimension = 7.5;
		power = 2;
		distanceDivider = 5;
		sideView.getTravelButton().addActionListener(new TravelButtonListener());
		sideView.getSaveButton().addActionListener(new SaveButtonListener());
	}
	
	/**
	 * @return String representation of UniverseSideViewController
	 */
	public String toString() {
		return "universe side view controller";
	}
	
	/**
	 * Button listener that handles traveling to a selected system
	 * 
	 * @author Evan
	 * @version 1.0
	 */
	private class TravelButtonListener implements ActionListener {
		
		/**
		 * travels to the selected system and updates the player's location, fuel, etc.
		 * @param e ActionEvent that represents the press of a button 
		 */
		public void actionPerformed(ActionEvent e) {
			SolarSystem ss;
			if (spaceTraders.getUniverseController().getUniverseView()
					.getSelectedSystem() != null) {
				
				ss = spaceTraders.getUniverseController()
						.getUniverseView().getSelectedSystem();
				
			} else {
				
				ss = spaceTraders.getCurrSystem();
				
			}
			final Point sysLocation = ss.getCoordinates();
			//checks to see if Player can travel to the system with current fuel. if he can, allow him to. otherwise, do nothing
			final UniverseController universeController =
					spaceTraders.getUniverseController();
			
			if (universeController.getUniverseView().getCircle().contains(
			sysLocation.x + imageDimension, sysLocation.y + imageDimension)) {
				
				System.out.println("Clicked!");
				universeController.loadSystem(ss);
				spaceTraders.setCurrSystem(ss);
				
				//get location before it's updated
			    final Point startLocation = universeController
			    		.getController().getPlayer().getCurrLocation();
			    
			    universeController.getController().getPlayer()
			    .setCurrLocation(ss.getCoordinates());
			
			    final Ship ship = universeController
			    		.getController().getPlayer().getShip();
			    
				final Point destLocation = ss.getCoordinates();
				
				//deducts fuel
				final int distance = (int) Math.sqrt(Math.pow(startLocation.x - 
				destLocation.x, power) + Math.pow(
				startLocation.y - destLocation.y, power));
				
				ship.setFuel(ship.getFuel() - distance / distanceDivider);
				
				System.out.println(ss.getAttributes().toString());
			
			} 
		}
		
		/**
		 * @return String representation of TravelButtonListener
		 */
		public String toString() {
			return "travel button listener";
		}
		
		
	}
	
	/**
	 * button listener that handles saving the game
	 * 
	 * @author wa
	 * @version 1.0
	 */
	private class SaveButtonListener implements ActionListener {

		/**
		 * @param e ActionEvent that represents the press of a button
		 */
		@Override
		public void actionPerformed(ActionEvent e) {
			spaceTraders.saveGame();
		}
		
		/**
		 * @return String representation of save button listener
		 */
		@Override
		public String toString() {
			return "save button listener";
		}
		
	}
}
